package com.alogic.html.xscript;

import com.alogic.html.xscript.HTML.HTMLElement;
import com.alogic.html.xscript.HTML.HTMLSimple;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * HTML文档
 * 
 * @author duanwaiwai
 * 
 * @version 1.6.12.36 [20190611 duanyy] <br>
 * - 重构html文档框架<br>
 *
 */
public class HTMLDoc extends HTMLElement{

	public HTMLDoc(String tag, Logiclet p) {
		super(tag, p);
	}
	
	@Override
	protected void onExecute(StringBuffer content, XsObject root, XsObject current, LogicletContext ctx,
			ExecuteWatcher watcher) {
        try {
            content.append("\n<html>");
            super.onSuperExecute(root, current, ctx, watcher);
        } finally {
            content.append("\n</html>");
        }
	}	
	
	public static class Head extends HTMLElement{
		public Head(String tag, Logiclet p) {
			super(tag, p);
		}
		
		@Override
		protected void onExecute(StringBuffer content, XsObject root, XsObject current, LogicletContext ctx,
				ExecuteWatcher watcher) {
	        try {
	            content.append("\n<head>");
	            super.onSuperExecute(root, current, ctx, watcher);
	        } finally {
	            content.append("\n</head>");
	        }
		}		
	}
	
	public static class Body extends HTMLElement{
		public Body(String tag, Logiclet p) {
			super(tag, p);
		}
		
		@Override
		protected void onExecute(StringBuffer content, XsObject root, XsObject current, LogicletContext ctx,
				ExecuteWatcher watcher) {
	        try {
	            content.append("\n<body>");
	            super.onSuperExecute(root, current, ctx, watcher);
	        } finally {
	            content.append("\n</body>");
	        }
		}		
	}
	
	public static class Title extends HTMLElement{
		protected String $title = "";
		
		public Title(String tag, Logiclet p) {
			super(tag, p);
		}
		
		@Override
		public void configure(Properties p){
			super.configure(p);
			
			$title = PropertiesConstants.getRaw(p,"text",$title);
		}
		
		@Override
		protected void onExecute(StringBuffer content, XsObject root, XsObject current, LogicletContext ctx,
				ExecuteWatcher watcher) {
			content.append("\n<title>").append(PropertiesConstants.transform(ctx, $title, "")).append("</title>");
		}		
	}
	
	public static class Br extends HTMLElement{
		public Br(String tag, Logiclet p) {
			super(tag, p);
		}
		
		@Override
		protected void onExecute(StringBuffer content, XsObject root, XsObject current, LogicletContext ctx,
				ExecuteWatcher watcher) {
			content.append("<br>");
		}		
	}	
	
	public static class Hr extends HTMLElement{
		public Hr(String tag, Logiclet p) {
			super(tag, p);
		}
		
		@Override
		protected void onExecute(StringBuffer content, XsObject root, XsObject current, LogicletContext ctx,
				ExecuteWatcher watcher) {
			content.append("<hr>");
		}		
	}
	
	public static class Heading extends HTMLSimple{
		public Heading(String tag, Logiclet p) {
			super(tag, p);
		}
		
		@Override
		protected String getHtmlTag(Properties p,String dft){
			return "h"+PropertiesConstants.getString(p,"level","1",true);
		}		
	}	
}